<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<sc-export-button :auth="'stock.export'" :apiObj="apiObj" :search="search"></sc-export-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-select v-model="search.ware_id" filterable placeholder="仓库" @change="inChange" clearable>
						<el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
					</el-select>
					<el-input v-model="search.goods_name" placeholder="产品型号" clearable @keyup.enter="upsearch"></el-input>
					<el-input v-model="search.goods_code" placeholder="产品编号" clearable @keyup.enter="upsearch"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" tableName="stockTable" :column="defaultColumn" show-summary remoteSummary row-key="id" @sort-change="onSortChange" stripe remoteSort :params="query" border>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
                <template #goods_name="{row}">
                    <div :style="'color:' + (row.is_short == 1 ? '#F00' : '')">{{row.goods_name}}</div>
                </template>
                <template #goods_code="{row}">
                    <div :style="'color:' + (row.is_short == 1 ? '#F00' : '')">{{row.goods_code}}</div>
                </template>
                <template #cate_name="{row}">
                    <div :style="'color:' + (row.is_short == 1 ? '#F00' : '')">{{row.cate_name}}</div>
                </template>
                <template #ware_name="{row}">
                    <div :style="'color:' + (row.is_short == 1 ? '#F00' : '')">{{row.ware_name}}</div>
                </template>
                <template #curr_stock="{row}">
                    <div :style="'color:' + (row.is_short == 1 ? '#F00' : '')">{{row.curr_stock}}</div>
                </template>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save = false"></save-dialog>

</template>

<script>
import saveDialog from './save'

export default {
	name: '查询1',
	components: {
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			apiObj: this.$API.goods.stock,
			selection: [],
			search: {
				ware_id: null,
				order_by: 'desc',
				field: 'id',
                goods_name: null,
                goods_code: null,
			},
			wareList: [],
			defaultColumn: [
				{
					label: "产品型号",
					prop: "goods_name",
				},
				{
					label: "产品编号",
					prop: "goods_code",
				},
				{
					label: "产品类型",
					prop: "cate_name",
				},
				{
					label: "仓库",
					prop: "ware_name",
				},
				{
					label: "库存数量",
					prop: "curr_stock",
					sortable: true
				},
			],
            query:{
                allocation: 1
            },
		}
	},
	mounted() {
		this.getWare()
	},
	methods: {
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		onSortChange(data) {
			if (data.order == 'ascending') {
				this.search.order_by = 'asc'
				this.search.field = data.prop
			}
			if (data.order == 'descending') {
				this.search.order_by = 'desc'
				this.search.field = data.prop
			}
			this.upsearch()
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh() {
			for (const key in this.search) {
				this.search[key] = null;
			}
			this.$refs.table.upData(this.search)
		},
		// 获取仓库
		async getWare() {
			var res = await this.$API.goods.ware.all.get();
			this.wareList = res.data;
		},
	}
}
</script>

<style></style>
